package com.auto.survey

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.activity.adjustment.AdjustmentActivity
import com.auto.survey.activity.bluetooth.BluetoolsActivity
import com.auto.survey.activity.bluetooth.BluetoothManagerUtil
import com.auto.survey.activity.config.ConfigActivity
import com.auto.survey.activity.monitoring.project.ManagerProjectActivity
import com.auto.survey.activity.test.TestActivity
import com.auto.survey.base.BaseActivity
import com.auto.survey.geocom.GeoComHelper


class MainActivity : BaseActivity() {

    companion object {
        const val REQUEST_CONNECT_DEVICE = 1
        val KEY_MENU_TITLE: String = "KEY_MENU_TITLE"
        val KEY_MENU_IMAGE: String = "KEY_MENU_IMAGE"
        val KEY_MENU_INTEN: String = "KEY_MENU_INTEN"
        val TAG:String = MainActivity::class.java.simpleName
    }

    // 消息列表
    private val bluetoothManager by lazy { BluetoothManagerUtil.getInstance(this) }
    private val geoComHelper: GeoComHelper by lazy { GeoComHelper.getInstance(bluetoothManager) }

    override fun initView() {
        bluetoothManager.bindService()
        geoComHelper.init()

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.blue)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.blue)
        mComment_title?.setTitle("首页")
        mComment_title?.setLeftIconVisible(View.GONE)

        val rvglid:RecyclerView = findViewById(R.id.id_RvGlidView)
        val rvlymanager:RecyclerView.LayoutManager = LinearLayoutManager(this@MainActivity)
        rvglid.layoutManager = rvlymanager
        val adapter = MainAdapter()
        adapter.setOnItemClickListener(object : MainAdapter.OnItemClickListener{
            override fun onItemClick(position: Int, map: Map<String, Any>) {
                try {
                    // 用户操作日志
                    val intent = Intent()
                    when (map[KEY_MENU_INTEN]) {
                        0x1111-> intent.setClass(this@MainActivity, ConfigActivity::class.java)
                        0x1112-> intent.setClass(this@MainActivity, BluetoolsActivity::class.java)
                        0x1113-> intent.setClass(this@MainActivity, TestActivity::class.java)
                        0x1114-> intent.setClass(this@MainActivity, AdjustmentActivity::class.java)
                        0x1115-> intent.setClass(this@MainActivity, ManagerProjectActivity::class.java)
                    }
                    startActivity(intent)
                } catch (e:Exception) {
                    e.printStackTrace()
                }
            }

        })
        rvglid.adapter = adapter
        adapter.setDatas(menuData())

    }

    fun menuData(): MutableList<Map<String, Any>> {
        val datas: MutableList<Map<String, Any>> = ArrayList()
//        val titls = arrayOf("基础配置", "蓝牙连接", "功能测试", "平差", "自动化监测", "测量原理")
//        val imges = arrayOf(R.mipmap.login_bull_no_border, R.mipmap.login_bull_no_border, R.mipmap.login_bull_no_border,
//            R.mipmap.login_bull_no_border, R.mipmap.login_bull_no_border, R.mipmap.login_bull_no_border)
//        val targs = arrayOf(0x1111, 0x1112, 0x1113, 0x1114, 0x1115, 0x1116)



        val titls = arrayOf("自动化监测")
        val imges = arrayOf(R.mipmap.login_bull_no_border)
        val targs = arrayOf(0x1115)

        for (i in titls.indices) {
            val menu1 = HashMap<String, Any>()
            menu1[KEY_MENU_TITLE] = titls[i]
            menu1[KEY_MENU_IMAGE] = imges[i]
            menu1[KEY_MENU_INTEN] = targs[i]

            datas.add(menu1)
        }
        return datas;
    }

    override fun onDestroy() {
        super.onDestroy()
        bluetoothManager.unbindService()
        geoComHelper.release()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }
}